package com.haiheng.pullup

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.xuanyuan.pullup.ApkUtils

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit


@SuppressLint("GoogleAppIndexingApiWarning")
class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.btnTest).setOnClickListener(this)
        findViewById<Button>(R.id.btnJump).setOnClickListener(this)

        initData()
    }


    private fun initData() {
        Observable.interval(10, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
//            .observeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : io.reactivex.Observer<Long?> {
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: Long) {
                    pullApp()
                }

                override fun onError(e: Throwable) {

                }

                override fun onComplete() {

                }
            })
    }

    /**
     * 拉起其他的App
     */
    internal fun pullApp() {
//        ApkUtils.pullApp(this,"com.seaeverit.guardgate")

        ApkUtils.pullApp(this,"com.xuanyuan.pullupclient")
    }

    override fun onClick(v: View?) {
        if (v == null) {
            return
        }

        if (v.id == R.id.btnTest) {
            pullApp()
        } else if (v.id == R.id.btnJump) {
            startActivity(Intent(this, TestActivity::class.java))
        }
    }


}